#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const int N = 2e5 + 10;
#define INF 0x3f3f3f3f;
typedef long long int ll;
#define close(); std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
//----------------------------------------------------------------------------//
//原来a,b,c,d都为10
void solve()
{
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int aa = a;
    int bb = b;
    int cc = c;
    int dd = d;
    //对a
    int ans = 0;
    if(a != 10)
    {
        if(a == 300) ans++;
        else if(a == 20) ans++;
        else if(a == 100) ans++;
        else
        {
            //从10变道a
            a -= 10;
            int t1 = a / 100;
            a %= 100;
            int t2 = a / 10;
            a %= 10;
            int t3 = a;
            int res1 = t1 + t2 + t3;

            //aa变成300
            int cur = 300 - aa;
            int t4 = cur / 100;
            cur %= 100;
            int t5 = cur / 10;
            cur %= 10;
            int t6 = cur;
            int res2 = 1 + t4 + t5 + t6;

            int res3 = min(res2, res1);
            ans += res3;

        }

    }

    if(b != 10)
    {
        if(b == 300) ans++;
        else if(b == 20) ans++;
        else if(b == 100) ans++;
        else
        {
            //从10变道a
            b -= 10;
            int t1 = b / 100;
            b %= 100;
            int t2 = b / 10;
            b %= 10;
            int t3 = b;
            int res1 = t1 + t2 + t3;

            //aa变成300
            int cur = 300 - bb;
            int t4 = cur / 100;
            cur %= 100;
            int t5 = cur / 10;
            cur %= 10;
            int t6 = cur;
            int res2 = 1 + t4 + t5 + t6;

            int res3 = min(res2, res1);
            ans += res3;

        }

    }

    if(c != 10)
    {
        if(c == 300) ans++;
        else if(c == 20) ans++;
        else if(c == 100) ans++;
        else
        {
            //从10变道a
            c -= 10;
            int t1 = c / 100;
            c %= 100;
            int t2 = c / 10;
            c %= 10;
            int t3 = c;
            int res1 = t1 + t2 + t3;

            //aa变成300
            int cur = 300 - cc;
            int t4 = cur / 100;
            cur %= 100;
            int t5 = cur / 10;
            cur %= 10;
            int t6 = cur;
            int res2 = 1 + t4 + t5 + t6;

            int res3 = min(res2, res1);
            ans +=res3;

        }

    }

    if(d != 10)
    {
        if(d == 300) ans++;
        else if(d == 20) ans++;
        else if(d == 100) ans++;
        else
        {
            //从10变道a
            d -= 10;
            int t1 = d / 100;
            d %= 100;
            int t2 = d / 10;
            d %= 10;
            int t3 = d;
            int res1 = t1 + t2 + t3;

            //aa变成300
            int cur = 300 - dd;
            int t4 = cur / 100;
            cur %= 100;
            int t5 = cur / 10;
            cur %= 10;
            int t6 = cur;
            int res2 = 1 + t4 + t5 + t6;

            int res3 = min(res2, res1);
            ans += res3;

        }

    }
    cout<<ans<<'\n';




}

int main()
{
    close();
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}